from django.contrib.contenttypes.models import ContentType
from .models import Notification

def create_notification(user, verb, actor=None, target=None, data=None):
    """
    创建通知的核心方法
    :param user: 接收用户
    :param verb: 通知类型（comment/like等）
    :param actor: 触发用户（可为空）
    :param target: 关联对象（评论/文章等）
    :param data: 附加数据（JSON）
    """
    notification = Notification.objects.create(
        user=user,
        verb=verb,
        actor=actor,
        data=data or {}
    )

    if target:
        notification.target_ct = ContentType.objects.get_for_model(target)
        notification.target_id = target.id
        notification.save()

    return notification
